<?php

declare (strict_types=1);

namespace App\Model;

use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\SoftDeletes;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Qbhy\HyperfAuth\AuthAbility;
use Qbhy\HyperfAuth\Authenticatable;

/**
 * @property int $id
 * @property int $status
 * @property string $username
 * @property string $nickname
 * @property string $password
 * @property int $num
 * @property string $email
 * @property string $phone
 * @property string $apple_id
 * @property string $google_id
 * @property string $wechat_id
 * @property string $alipay_id
 * @property int $type
 * @property int $role
 * @property int $last_login_time
 * @property int $create_time
 * @property int $update_time
 * @property string $deleted_at
 */
class User extends Model implements Authenticatable
{
    use AuthAbility;


    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'status' => 'integer', 'num' => 'integer', 'type' => 'integer', 'role' => 'integer', 'last_login_time' => 'integer', 'create_time' => 'integer', 'update_time' => 'integer'];

    /**
     * 返回登录信息
     * @param $uid
     * @return Builder|\Hyperf\Database\Model\Model|object|null
     */
    public static function jwtLoginInfo($uid)
    {
        return self::where(['id' => $uid, 'status' => 1])->first();

    }


    /**
     * 生成唯一的用户外部id
     * @return float
     */
    public static function makeNum(): float
    {

        $num = rand(10000000, 99999999);
        $exists = self::where(['num' => $num])->exists();
        if ($exists) {
            return self::makeNum();
        } else {
            return $num;
        }

    }


    /**
     * 判断用户是否是订阅会员
     * @param $uid 用户id
     * @return bool
     */
    public static function role($uid)
    {

        $role = self::where(['id' => $uid])->value('role');

        if ($role == 1) {
            return true;
        } else {
            return false;
        }

    }

    /**
     *
     * @return bool
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function syncMember()
    {

        //此处时间宽限10分钟
        $time = time() - 600;

        $where = [
            ['become_due', '<=', $time],
            ['role', '=', '1']
        ];

        $ids = self::where($where)->select(['id'])->get()->toArray();

        logger('Crontab', 'crontab')->info('会员信息过期', [$ids]);

        User::where($where)->update(['role' => 0]);

        return true;

    }

    /**
     * 注册基本信息
     * @param array $baseInfo
     * @return int
     */
    public static function addRegisterUser(array $baseInfo)
    {

        var_dump($baseInfo);

        // 忙完了加事务
        $uid = self::insertGetId($baseInfo);
        $rel = ChatUser::addUser($uid);
        var_dump($rel);
        UserDatum::addUser($uid);

        return $uid;
    }

    public static function checkRole($uid)
    {



    }


}
